package com.xd.cloud.common.util;

import java.net.URL;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**   
 * 功能描述：调用 阿里云Api逆向解析poi的地理位置信息
 * @author: wangshanshan   
 * @date: 2017年12月21日 上午9:50:02 
 */
public class GeographyPosition {

    public static String getGeographyPositionApi(String lng, String lat){  
        //参数解释: 纬度,经度 type 001 (100代表道路，010代表POI，001代表门址，111可以同时显示前三项)  
        String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+lng+"&type=010";  
        String res = "";     
        try {     
            URL url = new URL(urlString);    
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();    
            conn.setDoOutput(true);    
            conn.setRequestMethod("POST");    
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));    
            String line;    
            while ((line = in.readLine()) != null) {
            	res += line+"\n";    
            }    
            in.close();    
        } catch (Exception e) {    
            System.out.println("error in wapaction,and e is " + e.getMessage());    
        }   
        //System.out.println(res);  
        return res;    
    } 
    
    public static String getPositionName(String lng, String lat) {
        String add = getGeographyPositionApi(lng, lat);  
        JSONObject jsonObject = JSONObject.parseObject(add);
        JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("addrList"));
        JSONObject json = JSONObject.parseObject(jsonArray.get(0).toString());  
        String arr[] = json.getString("admName").split(",");  
        //String address = arr[0]+arr[1]+arr[2]+json.getString("name");
        String address = arr[0]+arr[1]+arr[2];
        //System.out.println(lng+","+lat+"---------->>>>"+address);
        return address;
    }
    
	public static void main(String[] args) {
		getPositionName("120.732472","31.357071");
	}
}
